#include "configuredialog.h"
#include "ui_configuredialog.h"

#include <QMessageBox>


ConfigureDialog::ConfigureDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ConfigureDialog)
{
//    ui->setupUi(this);
//    Initailization();
    DeviceInformation selected(tr("USB-4704,BID#0").toStdWString().c_str());
    InstantAiCtrl* instantAiCtrl = InstantAiCtrl::Create();
    ErrorCode errorCode = instantAiCtrl->setSelectedDevice(selected);
    CheckError(errorCode);
    Array<ValueRange>* valueRanges = instantAiCtrl->getFeatures()->getValueRanges();
    configure.deviceName = tr("USB-4704,BID#0");
    configure.channelStart = 0;
    configure.channelCount = 2;
    configure.valueRange = valueRanges->getItem(5);
    instantAiCtrl->Dispose();
    this->accept();
}

ConfigureDialog::~ConfigureDialog()
{
    delete ui;
}

void ConfigureDialog::Initailization()
{
    //创造InstantAiCtrl对象
    InstantAiCtrl *instantAiCtrl = InstantAiCtrl::Create();

    //获取可用设备列表
    Array<DeviceTreeNode> *supportedDevices = instantAiCtrl->getSupportedDevices();

    //若无可用设备，发出警告
    if (supportedDevices->getCount() == 0)
    {
        QMessageBox::information(this, tr("警告"),tr("请连接设备！"));
    }
    else //否则，将可用设备显示到cmbDevice
    {
        for (int i = 0; i < supportedDevices->getCount(); i++)
        {
            DeviceTreeNode const &node = supportedDevices->getItem(i);
            ui->cmbDevice->addItem(QString::fromWCharArray(node.Description));
        }
        ui->cmbDevice->setCurrentIndex(0);
    }
    configure.profilePath = L"";
    //销毁InstantAiCtrl对象以及可用设备
    instantAiCtrl->Dispose();
    supportedDevices->Dispose();
}

void ConfigureDialog::on_cmbDevice_currentIndexChanged(int index)
{
    //清空所有选择的信息
    ui->cmbChannelCount->clear();
    ui->cmbChannelStart->clear();
    ui->cmbValueRange->clear();

    //获取所选择的设备的信息
    std::wstring description = ui->cmbDevice->currentText().toStdWString();
    DeviceInformation selected(description.c_str());

    //创造InstantAiCtrl对象
    InstantAiCtrl *instantAiCtrl = InstantAiCtrl::Create();

    //获取控制码
    ErrorCode errorCode = instantAiCtrl->setSelectedDevice(selected);
    //設置“確定”按鈕可用
    ui->OKBtn->setEnabled(true);

    //若控制码为不0，发出设备不可用警告
    if (errorCode != 0)
    {
        QString str;
        QString des = QString::fromStdWString(description);
        str.sprintf("错误：错误代码是 0x%x\n\
                       %s 已经被占用或者没有插入电脑 \n\
                      请选择另外的设备!",
                    errorCode, des.toUtf8().data());
        QMessageBox::information(this, "警告", str);
        ui->OKBtn->setEnabled(false);
        return;
    }

    //获取设备的通道数以及逻辑通道数
    int channelCount = (instantAiCtrl->getChannelCount() < 16) ? instantAiCtrl->getChannelCount() : 16;
    int logicChannelCount = instantAiCtrl->getChannelCount();

    //设置cmbChannelStart以及cmbChannelCount信息
    for (int i = 0; i < logicChannelCount; i++)
    {
        ui->cmbChannelStart->addItem(QString("%1").arg(i));
    }

    for (int i = 0; i < channelCount; i++)
    {
        ui->cmbChannelCount->addItem(QString("%1").arg(i + 1));
    }

    //获取设备可用的电压值的范围
    Array<ValueRange> *ValueRanges = instantAiCtrl->getFeatures()->getValueRanges();
    wchar_t vrgDescription[128]; //双字节码
    MathInterval ranges;

    //添加到cmbValueRange
    for (int i = 0; i < ValueRanges->getCount(); i++)
    {
        errorCode = AdxGetValueRangeInformation(ValueRanges->getItem(i),
                                                sizeof(vrgDescription), vrgDescription, &ranges, nullptr);
        CheckError(errorCode);
        QString str = QString::fromWCharArray(vrgDescription);
        ui->cmbValueRange->addItem(str);
    }

    //销毁InstantAiCtrl对象
    instantAiCtrl->Dispose();

//    Set the default value.设置默认值
    QString usb = "USB-4704,BID#0";
    if( description== usb.toStdWString())
    {
        ui->cmbChannelStart->setCurrentIndex(0);
        ui->cmbChannelCount->setCurrentIndex(1);
        ui->cmbValueRange->setCurrentIndex(6);
        ui->cmbChannelStart->setEnabled(false);
        ui->cmbChannelCount->setEnabled(false);
        ui->cmbValueRange->setEnabled(false);
    }
    else
    {
        ui->cmbChannelStart->setCurrentIndex(0);
        ui->cmbChannelCount->setCurrentIndex(1);
        ui->cmbValueRange->setCurrentIndex(5);
        ui->cmbChannelStart->setEnabled(false);
        ui->cmbChannelCount->setEnabled(false);
        ui->cmbValueRange->setEnabled(false);
    }

}

void ConfigureDialog::CheckError(ErrorCode errorCode)
{
    if (errorCode >= 0xE0000000 && errorCode != Success)
    {
        QString message = tr("发生错误，错误代码: 0x") +
            QString::number(errorCode, 16).right(8).toUpper();
        QMessageBox::information(this, "警告", message);
    }
}



void ConfigureDialog::on_OKBtn_clicked()
{
    //若没有可用设备，则退出应用程序
    if (ui->cmbDevice->count() == 0)
    {
        QMessageBox::information(this,"警告","找不到可用设备");
//        QCoreApplication::quit();
    }

    //将下拉框所选择的信息赋给DeviceInformation类的selected函数，使得系统知道要选择的设备
    std::wstring description = ui->cmbDevice->currentText().toStdWString();
    DeviceInformation selected(description.c_str());

     //创造InstantAiCtrl对象，该对象控制DAQ的InstantAi功能
    InstantAiCtrl* instantAiCtrl = InstantAiCtrl::Create();

    //获取设备对象的控制码  errorCode
    ErrorCode errorCode = instantAiCtrl->setSelectedDevice(selected);
    //检查控制码，设备不可用则给出错误码
    CheckError(errorCode);

    //获取所有下拉选择框的信息赋值到结构体对象configure
    Array<ValueRange>* valueRanges = instantAiCtrl->getFeatures()->getValueRanges();
    configure.deviceName = ui->cmbDevice->currentText();
    configure.channelStart = ui->cmbChannelStart->currentText().toInt();
    configure.channelCount = ui->cmbChannelCount->currentText().toInt();
    configure.valueRange = valueRanges->getItem(ui->cmbValueRange->currentIndex());

    instantAiCtrl->Dispose();
    this->accept();
}

void ConfigureDialog::on_cancelBtn_clicked()
{
    this->reject();
}
